<?php
/**
 * Created by PhpStorm.
 * User: minicart
 * Date: 2018/9/25
 * Time: 14:58
 */
namespace app\admin\service;

use app\models\DictionaryModel;
use app\models\TypeModel;

class ConfigService
{
    /**
     * @return array
     */
    public function dictionary()
    {
        try {
            $page = input('page',1);
            $listRows = input('limit',5);
            $type = input('type');
            $where = [];
            if($type)$where['type'] = $type;
//            $where['status'] = 1;
            $order = 'id desc';
            $customers = DictionaryModel::pageLists($where, $page,$listRows, $order);
            return $customers;
        } catch (\Exception $ex) {
            return ['code' => 1, 'msg' => $ex->getMessage()];
        }
    }

    /**
     * @return array
     */
    public function add()
    {
        try {
            $id = input('id','');
            $_data = input('post.');
            $data = array(
                'name'=>$_data['name'],
                'type'=>$_data['type'],
                'type_name'=>$_data['type_name'],
                'describe'=>$_data['describe'],
            );
            if($id){
                $data['id'] = $id;
                DictionaryModel::update($data);
                return ['code' => 0, 'msg' => "更新成功"];
            }else{
                DictionaryModel::create($data);
                return ['code' => 0, 'msg' => "添加成功"];
            }
        } catch (\Exception $ex) {
            return ['code' => 1, 'msg' => $ex->getMessage()];
        }
    }

    /**
     * @return array
     */
    public function type()
    {
        try {
            $page = input('page',1);
            $listRows = input('limit',5);
            $where = [];
            $order = 'sort,id asc';
            $customers = TypeModel::pageLists($where, $page,$listRows, $order);
            return $customers;
        } catch (\Exception $ex) {
            return ['code' => 1, 'msg' => $ex->getMessage()];
        }
    }

    /**
     * @return array
     */
    public function type_add()
    {
        try {
            $id = input('id','');
            $title = input('title','');
            if($id){
                TypeModel::update(['id'=>$id,'title'=>$title]);
                return ['code' => 0, 'msg' => "更新成功"];
            }else{
                TypeModel::create(['title'=>$title]);
                return ['code' => 0, 'msg' => "添加成功"];
            }
        } catch (\Exception $ex) {
            return ['code' => 1, 'msg' => $ex->getMessage()];
        }
    }
}